home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter10 / isohex10_3 / isohex10_3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-21  |  12.2 KB  |  491 lines

  1. /*****************************************************************************
  2. IsoHex10_3.cpp
  3. Ernest S. Pazera
  4. 30JUN2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. Art by Ari Feldman
  10. *****************************************************************************/
  11.  
  12. //////////////////////////////////////////////////////////////////////////////
  13. //INCLUDES
  14. //////////////////////////////////////////////////////////////////////////////
  15. #define WIN32_LEAN_AND_MEAN  
  16.  
  17. #include <windows.h>
  18. #include "GDICanvas.h"
  19. #include "ddraw.h"
  20. #include "DDFuncs.h"
  21. #include "TileSet.h"
  22.    
  23. //////////////////////////////////////////////////////////////////////////////
  24. //DEFINES
  25. //////////////////////////////////////////////////////////////////////////////
  26. //name for our window class
  27. #define WINDOWCLASS "ISOHEX10"
  28. //title of the application
  29. #define WINDOWTITLE "IsoHex 10-3, with art by Ari Feldman"
  30.  
  31. //map and tile constants
  32. const int TILEWIDTH=32;
  33. const int TILEHEIGHT=32;
  34. const int MAPWIDTH=18;
  35. const int MAPHEIGHT=18;
  36.  
  37. //panels
  38. const int MAPPANELX=12;
  39. const int MAPPANELY=12;
  40. const int MAPPANELWIDTH=MAPWIDTH*TILEWIDTH;
  41. const int MAPPANELHEIGHT=MAPHEIGHT*TILEHEIGHT;
  42.  
  43. const int TILEPANELX=604;
  44. const int TILEPANELY=12;
  45. const int TILEPANELCOLUMNS=6;
  46. const int TILEPANELROWS=18;
  47. const int TILEPANELWIDTH=TILEPANELCOLUMNS*TILEWIDTH;
  48. const int TILEPANELHEIGHT=TILEPANELROWS*TILEHEIGHT;
  49.  
  50. //////////////////////////////////////////////////////////////////////////////
  51. //PROTOTYPES
  52. //////////////////////////////////////////////////////////////////////////////
  53. bool Prog_Init();//game data initalizer
  54. void Prog_Loop();//main game loop
  55. void Prog_Done();//game clean up
  56.  
  57. void ShowMapPanel();//show the map panel
  58. void ShowTilePanel();//show the tile panel
  59.  
  60. //////////////////////////////////////////////////////////////////////////////
  61. //GLOBALS
  62. //////////////////////////////////////////////////////////////////////////////
  63. HINSTANCE hInstMain=NULL;//main application handle
  64. HWND hWndMain=NULL;//handle to our main window
  65.  
  66. //directdraw
  67. LPDIRECTDRAW7 lpdd=NULL;
  68. LPDIRECTDRAWSURFACE7 lpddsMain=NULL;
  69. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  70.  
  71. //tileset
  72. CTileSet tsTileSet;
  73.  
  74. //tile map
  75. int iTileMap[MAPWIDTH][MAPHEIGHT];
  76.  
  77. //tile selection
  78. int iTileTop=0;
  79. int iTileSelected=0;
  80.  
  81. //////////////////////////////////////////////////////////////////////////////
  82. //WINDOWPROC
  83. //////////////////////////////////////////////////////////////////////////////
  84. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  85. {
  86.     //which message did we get?
  87.     switch(uMsg)
  88.     {
  89.     case WM_LBUTTONDOWN:
  90.         {
  91.             //point to contain mouse coords
  92.             POINT ptMouse;
  93.             ptMouse.x=LOWORD(lParam);
  94.             ptMouse.y=HIWORD(lParam);
  95.  
  96.             //RECT used for zone checking
  97.             RECT rcZone;
  98.  
  99.             //other variables
  100.             int mapx=0;
  101.             int mapy=0;
  102.             int tilex=0;
  103.             int tiley=0;
  104.             int tilenum=0;
  105.  
  106.             //check the map panel
  107.             SetRect(&rcZone,MAPPANELX,MAPPANELY,MAPPANELX+MAPPANELWIDTH,MAPPANELY+MAPPANELHEIGHT);
  108.             if(PtInRect(&rcZone,ptMouse))
  109.             {
  110.                 //in map panel
  111.                 //calculate what tile mouse is on
  112.                 mapx=(ptMouse.x-MAPPANELX)/TILEWIDTH;
  113.                 mapy=(ptMouse.y-MAPPANELY)/TILEHEIGHT;
  114.  
  115.                 //change map tile to currently selected tile
  116.                 iTileMap[mapx][mapy]=iTileSelected;
  117.  
  118.                 return(0);//handled
  119.             }
  120.  
  121.             //check the tile panel
  122.             SetRect(&rcZone,TILEPANELX,TILEPANELY,TILEPANELX+TILEPANELWIDTH,TILEPANELY+TILEPANELHEIGHT);
  123.             if(PtInRect(&rcZone,ptMouse))
  124.             {
  125.                 //calculate which tile was selected
  126.                 tilex=(ptMouse.x-TILEPANELX)/TILEWIDTH;
  127.                 tiley=(ptMouse.y-TILEPANELY)/TILEHEIGHT;
  128.                 tilenum=iTileTop+tilex+tiley*TILEPANELCOLUMNS;
  129.  
  130.                 //check for valid tile
  131.                 if(tilenum<tsTileSet.GetTileCount())
  132.                 {
  133.                     //assign current tile
  134.                     iTileSelected=tilenum;
  135.                 }
  136.  
  137.                 return(0);//handled
  138.             }
  139.  
  140.             //scroll tileset up
  141.             SetRect(&rcZone,TILEPANELX,0,TILEPANELX+TILEPANELWIDTH,TILEPANELY);
  142.             if(PtInRect(&rcZone,ptMouse))
  143.             {
  144.                 //check if we can scroll up
  145.                 if(iTileTop>0)
  146.                 {
  147.                     //scroll up
  148.                     iTileTop-=TILEPANELCOLUMNS;
  149.                 }
  150.             }
  151.             //scroll tileset down
  152.             SetRect(&rcZone,TILEPANELX,TILEPANELY+TILEPANELHEIGHT,TILEPANELX+TILEPANELWIDTH,600);
  153.             if(PtInRect(&rcZone,ptMouse))
  154.             {
  155.                 //check if we can scroll down
  156.                 if((iTileTop+TILEPANELCOLUMNS)<tsTileSet.GetTileCount())
  157.                 {
  158.                     //scroll up
  159.                     iTileTop+=TILEPANELCOLUMNS;
  160.                 }
  161.             }
  162.             return(0);//handled
  163.         }break;
  164.     case WM_MOUSEMOVE:
  165.         {
  166.             //if the left button is down
  167.             if(wParam & MK_LBUTTON)
  168.             {
  169.             //point to contain mouse coords
  170.             POINT ptMouse;
  171.             ptMouse.x=LOWORD(lParam);
  172.             ptMouse.y=HIWORD(lParam);
  173.  
  174.             //RECT used for zone checking
  175.             RECT rcZone;
  176.  
  177.             //other variables
  178.             int mapx=0;
  179.             int mapy=0;
  180.             int tilex=0;
  181.             int tiley=0;
  182.             int tilenum=0;
  183.  
  184.             //check the map panel
  185.             SetRect(&rcZone,MAPPANELX,MAPPANELY,MAPPANELX+MAPPANELWIDTH,MAPPANELY+MAPPANELHEIGHT);
  186.             if(PtInRect(&rcZone,ptMouse))
  187.             {
  188.                 //in map panel
  189.                 //calculate what tile mouse is on
  190.                 mapx=(ptMouse.x-MAPPANELX)/TILEWIDTH;
  191.                 mapy=(ptMouse.y-MAPPANELY)/TILEHEIGHT;
  192.  
  193.                 //change map tile to currently selected tile
  194.                 iTileMap[mapx][mapy]=iTileSelected;
  195.  
  196.                 return(0);//handled
  197.             }
  198.  
  199.             //check the tile panel
  200.             SetRect(&rcZone,TILEPANELX,TILEPANELY,TILEPANELX+TILEPANELWIDTH,TILEPANELY+TILEPANELHEIGHT);
  201.             if(PtInRect(&rcZone,ptMouse))
  202.             {
  203.                 //calculate which tile was selected
  204.                 tilex=(ptMouse.x-TILEPANELX)/TILEWIDTH;
  205.                 tiley=(ptMouse.y-TILEPANELY)/TILEHEIGHT;
  206.                 tilenum=iTileTop+tilex+tiley*TILEPANELCOLUMNS;
  207.  
  208.                 //check for valid tile
  209.                 if(tilenum<tsTileSet.GetTileCount())
  210.                 {
  211.                     //assign current tile
  212.                     iTileSelected=tilenum;
  213.                 }
  214.             }
  215.             }
  216.             return(0);//handled
  217.         }break;
  218.     case WM_KEYDOWN:
  219.         {
  220.             //on escape, destroy main window
  221.             if(wParam==VK_ESCAPE)
  222.             {
  223.                 DestroyWindow(hWndMain);
  224.             }
  225.  
  226.             return(0);//handled
  227.         }break;
  228.     case WM_DESTROY://the window is being destroyed
  229.         {
  230.  
  231.             //tell the application we are quitting
  232.             PostQuitMessage(0);
  233.  
  234.             //handled message, so return 0
  235.             return(0);
  236.  
  237.         }break;
  238.     case WM_PAINT://the window needs repainting
  239.         {
  240.             //a variable needed for painting information
  241.             PAINTSTRUCT ps;
  242.             
  243.             //start painting
  244.             HDC hdc=BeginPaint(hwnd,&ps);
  245.  
  246.             /////////////////////////////
  247.             //painting code would go here
  248.             /////////////////////////////
  249.  
  250.             //end painting
  251.             EndPaint(hwnd,&ps);
  252.                         
  253.             //handled message, so return 0
  254.             return(0);
  255.         }break;
  256.     }
  257.  
  258.     //pass along any other message to default message handler
  259.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  260. }
  261.  
  262.  
  263. //////////////////////////////////////////////////////////////////////////////
  264. //WINMAIN
  265. //////////////////////////////////////////////////////////////////////////////
  266. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  267. {
  268.     //assign instance to global variable
  269.     hInstMain=hInstance;
  270.  
  271.     //create window class
  272.     WNDCLASSEX wcx;
  273.  
  274.     //set the size of the structure
  275.     wcx.cbSize=sizeof(WNDCLASSEX);
  276.  
  277.     //class style
  278.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  279.  
  280.     //window procedure
  281.     wcx.lpfnWndProc=TheWindowProc;
  282.  
  283.     //class extra
  284.     wcx.cbClsExtra=0;
  285.  
  286.     //window extra
  287.     wcx.cbWndExtra=0;
  288.  
  289.     //application handle
  290.     wcx.hInstance=hInstMain;
  291.  
  292.     //icon
  293.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  294.  
  295.     //cursor
  296.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  297.  
  298.     //background color
  299.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  300.  
  301.     //menu
  302.     wcx.lpszMenuName=NULL;
  303.  
  304.     //class name
  305.     wcx.lpszClassName=WINDOWCLASS;
  306.  
  307.     //small icon
  308.     wcx.hIconSm=NULL;
  309.  
  310.     //register the window class, return 0 if not successful
  311.     if(!RegisterClassEx(&wcx)) return(0);
  312.  
  313.     //create main window
  314.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  315.  
  316.     //error check
  317.     if(!hWndMain) return(0);
  318.  
  319.     //if program initialization failed, then return with 0
  320.     if(!Prog_Init()) return(0);
  321.  
  322.     //message structure
  323.     MSG msg;
  324.  
  325.     //message pump
  326.     for(;;)    
  327.     {
  328.         //look for a message
  329.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  330.         {
  331.             //there is a message
  332.  
  333.             //check that we arent quitting
  334.             if(msg.message==WM_QUIT) break;
  335.             
  336.             //translate message
  337.             TranslateMessage(&msg);
  338.  
  339.             //dispatch message
  340.             DispatchMessage(&msg);
  341.         }
  342.  
  343.         //run main game loop
  344.         Prog_Loop();
  345.     }
  346.     
  347.     //clean up program data
  348.     Prog_Done();
  349.  
  350.     //return the wparam from the WM_QUIT message
  351.     return(msg.wParam);
  352. }
  353.  
  354. //////////////////////////////////////////////////////////////////////////////
  355. //INITIALIZATION
  356. //////////////////////////////////////////////////////////////////////////////
  357. bool Prog_Init()
  358. {
  359.     //create IDirectDraw7
  360.     lpdd=LPDD_Create(hWndMain,DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
  361.  
  362.     //set display mode
  363.     lpdd->SetDisplayMode(800,600,16,0,0);
  364.  
  365.     //create primary surface
  366.     lpddsMain=LPDDS_CreatePrimary(lpdd,1);
  367.  
  368.     //get back buffer
  369.     lpddsBack=LPDDS_GetSecondary(lpddsMain);
  370.  
  371.     //clear out back buffer
  372.     DDBLTFX ddbltfx;
  373.     DDBLTFX_ColorFill(&ddbltfx,0);
  374.     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  375.  
  376.     //load in tileset
  377.     tsTileSet.Load(lpdd,"IsoHex10_3.bmp");
  378.  
  379.     //clear out the map
  380.     for(int mapx=0;mapx<MAPWIDTH;mapx++)
  381.     {
  382.         for(int mapy=0;mapy<MAPHEIGHT;mapy++)
  383.         {
  384.             iTileMap[mapx][mapy]=0;
  385.         }
  386.     }
  387.  
  388.     return(true);//return success
  389. }
  390.  
  391. //////////////////////////////////////////////////////////////////////////////
  392. //CLEANUP
  393. //////////////////////////////////////////////////////////////////////////////
  394. void Prog_Done()
  395. {
  396.     //destroy primary surface
  397.     LPDDS_Release(&lpddsMain);
  398.  
  399.     //destroy IDirectDraw7
  400.     LPDD_Release(&lpdd);
  401. }
  402.  
  403. //////////////////////////////////////////////////////////////////////////////
  404. //MAIN GAME LOOP
  405. //////////////////////////////////////////////////////////////////////////////
  406. void Prog_Loop()
  407. {
  408.     //show the map panel
  409.     ShowMapPanel();
  410.     //show tile selection panel
  411.     ShowTilePanel();
  412.     //flip
  413.     lpddsMain->Flip(NULL,DDFLIP_WAIT);
  414. }
  415.  
  416. //show the map panel
  417. void ShowMapPanel()
  418. {
  419.     //clear out map panel
  420.     //set up fill rect
  421.     RECT rcFill;
  422.     SetRect(&rcFill,MAPPANELX,MAPPANELY,MAPPANELX+MAPPANELWIDTH,MAPPANELY+MAPPANELHEIGHT);
  423.     //set up ddbltfx
  424.     DDBLTFX ddbltfx;
  425.     DDBLTFX_ColorFill(&ddbltfx,0);
  426.     lpddsBack->Blt(&rcFill,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  427.  
  428.     //loop through map
  429.     for(int mapy=0;mapy<MAPHEIGHT;mapy++)
  430.     {
  431.         for(int mapx=0;mapx<MAPWIDTH;mapx++)
  432.         {
  433.             //put the tile
  434.             tsTileSet.PutTile(lpddsBack,MAPPANELX+mapx*TILEWIDTH,MAPPANELY+mapy*TILEHEIGHT,iTileMap[mapx][mapy]);
  435.         }
  436.     }
  437. }
  438.  
  439. //show the tile panel
  440. void ShowTilePanel()
  441. {
  442.     //clear out map panel
  443.     //set up fill rect
  444.     RECT rcFill;
  445.     SetRect(&rcFill,TILEPANELX,TILEPANELY,TILEPANELX+TILEPANELWIDTH,TILEPANELY+TILEPANELHEIGHT);
  446.     //set up ddbltfx
  447.     DDBLTFX ddbltfx;
  448.     DDBLTFX_ColorFill(&ddbltfx,0);
  449.     lpddsBack->Blt(&rcFill,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  450.  
  451.     //set tile counter to first tile
  452.     int tilenum=iTileTop;
  453.     //loop through columns and rows
  454.     for(int tiley=0;tiley<TILEPANELROWS;tiley++)
  455.     {
  456.         for(int tilex=0;tilex<TILEPANELCOLUMNS;tilex++)
  457.         {
  458.             //check for tilenum's existence in tileset
  459.             if(tilenum<tsTileSet.GetTileCount())
  460.             {
  461.                 tsTileSet.PutTile(lpddsBack,TILEPANELX+tilex*TILEWIDTH,TILEPANELY+tiley*TILEHEIGHT,tilenum);
  462.                 //check for selected tile
  463.                 if(tilenum==iTileSelected)
  464.                 {
  465.                     //grab the dc
  466.                     HDC hdc;
  467.                     lpddsBack->GetDC(&hdc);
  468.                     //calculate outline rect
  469.                     RECT rcOutline;
  470.                     SetRect(&rcOutline,TILEPANELX+tilex*TILEWIDTH,TILEPANELY+tiley*TILEHEIGHT,TILEPANELX+tilex*TILEWIDTH+TILEWIDTH,TILEPANELY+tiley*TILEHEIGHT+TILEHEIGHT);
  471.  
  472.                     //select a white pen into dc
  473.                     SelectObject(hdc,(HPEN)GetStockObject(WHITE_PEN));
  474.  
  475.                     //place selection rectangle
  476.                     MoveToEx(hdc,rcOutline.left,rcOutline.top,NULL);
  477.                     LineTo(hdc,rcOutline.right-1,rcOutline.top);
  478.                     LineTo(hdc,rcOutline.right-1,rcOutline.bottom-1);
  479.                     LineTo(hdc,rcOutline.left,rcOutline.bottom-1);
  480.                     LineTo(hdc,rcOutline.left,rcOutline.top);
  481.  
  482.                     //release the dc
  483.                     lpddsBack->ReleaseDC(hdc);
  484.                 }
  485.             }
  486.             //increase tile counter
  487.             tilenum++;
  488.         }
  489.     }
  490. }
  491.